import java.util.Comparator;
import java.util.Objects;

class Student1 implements Comparable<Student1>{
    public String name;
    public int age;

    public Student1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student1 student1 = (Student1) o;
        return age == student1.age && Objects.equals(name, student1.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }


    @Override
    public int compareTo(Student1 o) {
        //return this.age-o.age;
        return this.name.compareTo(o.name);
    }
}
class AgeComparator implements Comparator<Student1>{
    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.age-o2.age;
    }
}
class NameComparator implements Comparator<Student1>{
    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test2 {
    public static void main(String[] args) {
        //比较
        /*
        1，equals返回值是boolean类型的所以只能比较是否相同(编译器自动生成)
        2，使类接上一个comparable接口，重写compareTo方法，缺点，写死了不能灵活的进行比较
        3，比较器进行比较,需要实现一个比较器对象
         */
        Student1 student1 = new Student1("lihua", 12);
        Student1 student11 = new Student1("zhangsan", 14);
        System.out.println(student1.equals(student11));
        System.out.println(student1.compareTo(student11));
        AgeComparator ageComparator=new AgeComparator();
        NameComparator nameComparator=new NameComparator();
        System.out.println(ageComparator.compare(student1, student11));
        System.out.println(nameComparator.compare(student1, student11));
    }
}
